package zl.ordermanager.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import zl.ordermanager.dao.SalesOrderDao;
import zl.ordermanager.po.Tsalesorder;
import zl.ordermanager.service.SalesOrderService;
import zl.ordermanager.vo.DataGrid;
import zl.ordermanager.vo.PageHelper;
import zl.ordermanager.vo.SalesOrder;

@Service
public class SalesOrderServiceImpl implements SalesOrderService {

	@Autowired
	private SalesOrderDao salesOrderDao;

	@Override
	public DataGrid<SalesOrder> dataGrid(PageHelper pageHelper) {
		DataGrid<SalesOrder> dataGrid = new DataGrid<SalesOrder>();
		List<SalesOrder> salesOrderList = new ArrayList<SalesOrder>();
		Map<String, Object> params = new HashMap<String, Object>();
		if (pageHelper.getKeywords() != null && !pageHelper.getKeywords().isEmpty()) {
			params.put("keyword", "%%" + pageHelper.getKeywords() + "%%");
		}
		if (pageHelper.getCustomer() != null && !pageHelper.getCustomer().isEmpty()) {
			params.put("customer", "%%" + pageHelper.getCustomer() + "%%");
		}
		if (pageHelper.getAbstractMonth() != null && !pageHelper.getAbstractMonth().isEmpty()) {
			params.put("orderDate", "%%" + pageHelper.getAbstractMonth() + "%%");
		}
		if (pageHelper.getSalesman() != null && !pageHelper.getSalesman().isEmpty()) {
			params.put("salesman", pageHelper.getSalesman());
		}
		String hql = " from Tsalesorder t ";
		List<Tsalesorder> tsalesorderList = salesOrderDao.find(hql + whereHql(pageHelper) + orderHql(pageHelper),
				params, pageHelper.getPageNum(), pageHelper.getNumPerPage());
		if (tsalesorderList != null && tsalesorderList.size() > 0) {
			for (Tsalesorder tsalesorder : tsalesorderList) {
				SalesOrder salesOrder = new SalesOrder();
				BeanUtils.copyProperties(tsalesorder, salesOrder);
				salesOrderList.add(salesOrder);
			}
		}
		dataGrid.setList(salesOrderList);
		dataGrid.setTotal(salesOrderDao.count("select count(*) " + hql + whereHql(pageHelper), params));
		return dataGrid;
	}

	private String whereHql(PageHelper pageHelper) {
		String whereString = "";
		if (pageHelper.getKeywords() != null && !pageHelper.getKeywords().isEmpty()) {
			whereString += " where t.orderNo like :keyword ";
			whereString += " or t.orderDate like :keyword ";
			whereString += " or t.customer like :keyword ";
			whereString += " or t.salesman like :keyword ";
			whereString += " or t.money like :keyword ";
			whereString += " or t.delivery like :keyword ";
			whereString += " or t.invoice like :keyword ";
		}

		if (pageHelper.getCustomer() != null && !pageHelper.getCustomer().isEmpty()) {
			if (whereString.isEmpty()) {
				whereString = "where (t.customer like :customer)";
			} else {
				whereString += " and (t.customer like :customer)";
			}
		}
		if (pageHelper.getAbstractMonth() != null && !pageHelper.getAbstractMonth().isEmpty()) {
			if (whereString.isEmpty()) {
				whereString = "where (t.orderDate like :orderDate)";
			} else {
				whereString += " and (t.orderDate like :orderDate)";
			}
		}
		if (pageHelper.getSalesman() != null && !pageHelper.getSalesman().isEmpty()) {
			if (whereString.isEmpty()) {
				whereString = "where (t.salesman = :salesman)";
			} else {
				whereString += " and (t.salesman = :salesman)";
			}
		}

		return whereString;
	}

	private String orderHql(PageHelper pageHelper) {
		String orderString = "";
		if (pageHelper.getOrderField() != null && !pageHelper.getOrderField().isEmpty()
				&& pageHelper.getOrderDirection() != null && !pageHelper.getOrderDirection().isEmpty()) {
			orderString = " order by t." + pageHelper.getOrderField() + " " + pageHelper.getOrderDirection();
		}
		return orderString;
	}

	@Override
	public SalesOrder get(Integer id) {
		Tsalesorder tsalesorder = salesOrderDao.get(Tsalesorder.class, id);
		;
		if (tsalesorder == null) {
			return null;
		}
		SalesOrder salesOrder = new SalesOrder();
		BeanUtils.copyProperties(tsalesorder, salesOrder);
		return salesOrder;
	}

	@Override
	public SalesOrder getByOrderNo(String orderNo) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("orderNo", orderNo);
		Tsalesorder tsalesorder = salesOrderDao.get("from Tsalesorder t where t.orderNo = :orderNo", params);
		if (tsalesorder == null) {
			return null;
		}
		SalesOrder salesOrder = new SalesOrder();
		BeanUtils.copyProperties(tsalesorder, salesOrder);
		return salesOrder;
	}

	@Override
	public void add(SalesOrder salesOrder) throws Exception {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("orderNo", salesOrder.getOrderNo());
		if (salesOrderDao.count("select count(*) from Tsalesorder t where t.orderNo = :orderNo", params) > 0) {
			throw new Exception("订单号已存在！");
		}
		Tsalesorder tsalesorder = new Tsalesorder();
		BeanUtils.copyProperties(salesOrder, tsalesorder);
		salesOrderDao.save(tsalesorder);
	}

	@Override
	public void edit(SalesOrder salesOrder) throws Exception {
		Tsalesorder tsalesorder = salesOrderDao.get(Tsalesorder.class, salesOrder.getId());
		BeanUtils.copyProperties(salesOrder, tsalesorder);
	}

	@Override
	public void delete(Integer id) {
		salesOrderDao.delete(salesOrderDao.get(Tsalesorder.class, id));
	}

}
